package com.doubao.user.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 用户请求头拦截器
 * 负责处理用户请求头和认证信息
 */
@Slf4j
public class UserHeaderInterceptor implements HandlerInterceptor {

    private static final String USER_ID_HEADER = "X-User-ID";
    private static final String TOKEN_HEADER = "Authorization";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 不需要特殊处理，我们直接从请求中获取用户信息
        // UserContext已经被修改为直接从请求头中获取用户ID和Token
        
        // 如果需要调试，可以打印请求头信息
        if (log.isDebugEnabled()) {
            String userId = request.getHeader(USER_ID_HEADER);
            String token = request.getHeader(TOKEN_HEADER);
            log.debug("Request from user: {}, token: {}", userId, token != null ? "present" : "absent");
        }
        
        return true;
    }
} 